/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ecy.quadratic.ui.presenter;

import android.content.Context;

import com.ecy.quadratic.api.ComicApi;
import com.ecy.quadratic.beans.ComicClassifyRecommend;
import com.ecy.quadratic.beans.ComicRecommend;
import com.ecy.quadratic.beans.ComicRecommendResponseBean;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.manager.SettingManager;
import com.ecy.quadratic.reader.base.RxPresenter;
import com.ecy.quadratic.reader.utils.LogUtils;
import com.ecy.quadratic.reader.utils.RxUtil;
import com.ecy.quadratic.reader.utils.StringUtils;
import com.ecy.quadratic.ui.contract.ComicRecommendContract;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;

/**
 * @author yuyh.
 * @date 2016/8/3.
 */
public class ComicRecommendPresenter extends RxPresenter<ComicRecommendContract.View>
        implements ComicRecommendContract.Presenter<ComicRecommendContract.View> {

    private Context mContext;
    private ComicApi comicApi;

    @Inject
    public ComicRecommendPresenter(Context mContext, ComicApi comicApi) {
        this.mContext = mContext;
        this.comicApi = comicApi;
    }

    @Override
    public void getRecommendList(String page) {
        String key = StringUtils.creatAcacheKey("comic_recommend-list", page, SettingManager.getInstance().getUserChooseSex());
        Observable<ComicRecommendResponseBean> fromNetWork = comicApi.getComicRecommend(page)
                .compose(RxUtil.<ComicRecommendResponseBean>rxCacheBeanHelper(key));

        //依次检查disk、network
        Subscription rxSubscription = Observable.concat(RxUtil.rxCreateDiskObservable(key, ComicRecommendResponseBean.class), fromNetWork)
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<ComicRecommendResponseBean, ComicRecommendResponseBean>() {
                    @Override
                    public ComicRecommendResponseBean call(ComicRecommendResponseBean old) {
                        List<ComicClassifyRecommend> list = new ArrayList<>();
                        list.add(new ComicClassifyRecommend("recommend", "热门推荐"));
                        list.add(new ComicClassifyRecommend("fantasy", "玄幻魔界"));
                        list.add(new ComicClassifyRecommend("ancient", "古风侠情"));
                        list.add(new ComicClassifyRecommend("city", "都市"));
                        list.add(new ComicClassifyRecommend("thriller", "惊悚恐怖"));
                        list.add(new ComicClassifyRecommend("city", "生活"));
                        list.add(new ComicClassifyRecommend("across", "穿越"));
                        list.add(new ComicClassifyRecommend("joke", "搞笑"));
                        list.add(new ComicClassifyRecommend("tanbi", "耽美同人"));
                        list.add(new ComicClassifyRecommend("guess", "猜你喜欢"));

                        if (old != null && old.getResult() != null && old.getResult().list != null) {
                            list.get(0).recommends = old.getResult().list.subList(0, 9);
                            list.get(1).recommends = old.getResult().list.subList(9, 17);
                            list.get(2).recommends = old.getResult().list.subList(19, 30);
                            list.get(3).recommends = old.getResult().list.subList(19, 30);
                            list.get(4).recommends = old.getResult().list.subList(13, 20);
                            list.get(5).recommends = old.getResult().list.subList(11, 20);
                            list.get(6).recommends = old.getResult().list.subList(19, 30);
                            list.get(7).recommends = old.getResult().list.subList(13, 20);
                            list.get(8).recommends = old.getResult().list.subList(11, 20);
                            list.get(9).recommends = old.getResult().list.subList(8, 23);
                            old.setClassifyRecommends(list);
                        }

                        return old;
                    }
                })
                .subscribe(new Observer<ComicRecommendResponseBean>() {
                    @Override
                    public void onNext(ComicRecommendResponseBean recommend) {
                        if (recommend != null) {
                            PageInfo<ComicRecommend> list = recommend.getResult();
                            if (list != null && mView != null) {
                                mView.showRecommendList(list, recommend.getClassifyRecommends());
                            }
                        }
                    }

                    @Override
                    public void onCompleted() {
                        mView.complete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e("getRecommendList", e.toString());
                        mView.showError();
                    }
                });
        addSubscrebe(rxSubscription);
    }
}
